﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.Interfaces.ImageCapture;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CommonLibrary.Classes.ImageCapture
{
    public class ScreenCapture : IImageCapture<Screen>
    {
        private Graphics _screenImage;
        private ImageFormat _screenImageFormat;
        private Bitmap _screenBitMap;

        public Graphics ScreenImage
        {
            get
            {
                return _screenImage;
            }
           
        }

        public ImageFormat ScreenImageFormat
        {
            get
            {
                return _screenImageFormat;
            }
           
        }

        public Bitmap ScreenBitMap
        {
            get
            {
                return _screenBitMap;
            }
          
        }

        public bool CaptureImage(Screen capture)
        {
            bool valid = true;

            try
            {
                _screenBitMap = new Bitmap(capture.Bounds.Width, capture.Bounds.Height, PixelFormat.Format32bppArgb);
                _screenImage = Graphics.FromImage(_screenBitMap);
                _screenImage.CopyFromScreen(capture.Bounds.X, capture.Bounds.Y,0,0, capture.Bounds.Size, CopyPixelOperation.SourceCopy);
                if (_screenImageFormat == null)
                {
                    _screenImageFormat = ImageFormat.Png;
                }
            }
            catch (Exception error)
            {
                valid = false;
                throw new InvalidOperationException("Unable to capture image", error);
            }


            return valid;
        }

        public ScreenCapture()
        {
        }

        public ScreenCapture(ImageFormat imageFormat)
        {
            _screenImageFormat = imageFormat;
        }

        /// <summary>
        /// This will save an image to a file system. 
        /// </summary>
        /// <param name="pathWithFileName">full path and file name </param>
        /// <returns>Returan a value indicating if the save completed successfully</returns>
        public bool SaveImage(string pathWithFileName)
        {
            bool valid = true;
            try
            {
                _screenBitMap.Save(pathWithFileName, _screenImageFormat);
            }
            catch (Exception error)
            {
                valid = false;
                throw new InvalidOperationException("Unable to save the image to the specified location", error);
            }

            return valid;
        }

        public byte[] ConvertToByteArray()
        {
            byte[] returnArray = null;

            try
            {
                MemoryStream imageMemStream = new MemoryStream();
                _screenBitMap.Save(imageMemStream, _screenImageFormat);
                returnArray =  imageMemStream.ToArray();
            }
            catch (Exception error)
            {
                throw new InvalidCastException("Unable to convert image in byte array",error);
            }


            return returnArray;
        }
    }
}
